package com.ruoyi.system.factory;

import com.arcsoft.face.FaceEngine;
import com.ruoyi.common.config.FaceConfig;
import com.ruoyi.common.constant.StaticObj;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.springframework.boot.system.ApplicationHome;

/**
 * @author lzw
 * 人脸引擎池工厂类
 */
public class FaceEngineFactory extends BasePooledObjectFactory<FaceEngine> {

    @Override
    public FaceEngine create() {
        ApplicationHome applicationHome = new ApplicationHome();
        String absolutePath = applicationHome.getDir().getAbsolutePath();
        FaceEngine faceEngine = new FaceEngine(absolutePath + "/libs");
        faceEngine.activeOnline(FaceConfig.APP_ID, FaceConfig.SDK_KEY);
        faceEngine.init(StaticObj.DEFAULT_ENGINE_CONFIG);
        return faceEngine;
    }

    @Override
    public PooledObject<FaceEngine> wrap(FaceEngine faceEngine) {
        return new DefaultPooledObject<>(faceEngine);
    }

    @Override
    public void destroyObject(PooledObject<FaceEngine> p) throws Exception {
        FaceEngine faceEngine = p.getObject();
        faceEngine.unInit();
        super.destroyObject(p);
    }
}
